﻿'===============================================================
'JMatcher 
'---------------------------------------------------------------
'Purpose : Finds the job offers that is "right" for the user. The 
'          user's variables passed in the Constructor. 
'          The getOffers functions is reponsible for the interaction      
'          of the class.
'
' Author : John Paraskevopoulos, 8 December 2012
'===============================================================

Imports System.Data.OleDb

Public Class JOfferMatcher

    Private userDegreeID
    Private userCity
    Private userCountryID
    Private userMinFee
    Private userMaster
    Private userPhd
    Private userFL
    Private userArmy
    Private userTypeOfferID



    Public Sub New(ByVal employee As DatabaseDataSet.EmployeeRow, ByVal userTypeOfferID As Integer, ByVal userCity As String, ByVal userCountryID As Integer, ByVal userMinFee As Integer)

        Me.userDegreeID = employee.Item("EmployeeDegreeID")        
        Me.userMaster = employee.Item("EmployeeMaster")
        Me.userPhd = employee.Item("EmployeePhd")
        Me.userFL = employee.Item("EmployeeForeignLanguage")
        Me.userArmy = employee.Item("EmployeeArmy")
        Me.userTypeOfferID = userTypeOfferID
        Me.userCity = userCity
        Me.userCountryID = userCountryID
        Me.userMinFee = userMinFee

    End Sub


    '===============================================================
    'JMatcher - public Function getOffers
    '---------------------------------------------------------------
    'Purpose : Creates a connection to database. Retrieves the offers
    '          that match with the current user(private fields) and
    '          returns a datatable filled with them in descending order.
    '          Uses the private function calcRank.
    '
    ' Author : John Paraskevopoulos, 8 December 2012
    '===============================================================
    Public Function getOffers() As DataTable
        Dim query1 As String = "SELECT jo.*, DegreesDistance.*, JobTypesDistance.* FROM (JobOffer AS jo " & _
        "INNER JOIN DegreesDistance ON (DegreesDistance.DegreesDistanceFirstDegreeID = jo.JobOfferDegreeID  AND DegreesDistance.DegreesDistanceSecondDegreeID = " & userDegreeID & ")) " & _
        "INNER JOIN JobTypesDistance ON (JobTypesDistance.JobTypesDistanceFirstJobTypeID = jo.JobOfferJobTypeID AND JobTypesDistance.JobTypesDistanceSecondJobTypeID = " & userTypeOfferID & ") " & _
        "WHERE jo.JobOfferCity = '" & userCity & "' AND jo.JobOfferCountryID = " & userCountryID & " AND jo.JobOfferSalary >= " & userMinFee

        Dim cn As OleDbConnection = New OleDbConnection
        Dim cmd As New OleDbCommand
        Dim dtJobOffer As DataTable = New DataTable

        Try
            cn.ConnectionString = My.Settings.DatabaseConnectionString
            cn.Open()

            cmd.Connection = cn
            cmd.CommandType = CommandType.Text
            cmd.CommandText = query1
            Dim oleAd As OleDbDataAdapter = New OleDbDataAdapter(cmd)

            oleAd.Fill(dtJobOffer)

            Dim newColumn As DataColumn = New DataColumn
            newColumn.DataType = GetType(System.Int32)
            newColumn.ColumnName = "Rank"
            dtJobOffer.Columns.Add(newColumn)

        Catch ex As Exception
            MessageBox.Show("Cannot retrieve the offers - JMatcher class!!!" & ex.Message, "Connection to Database Error")
        Finally
            cn.Close()
        End Try

        For Each row As DataRow In dtJobOffer.Rows
            row.Item("Rank") = calcRank(row)
        Next row

        dtJobOffer.DefaultView.Sort = "Rank DESC"
        Return dtJobOffer
    End Function

    '===============================================================
    'JMatcher - private function calcRank
    '---------------------------------------------------------------
    'Purpose : Canculates the rank(percent) that reprecents how much
    '          an offer is right for the current user. Each row is an
    '          offer and is passed as a parameter. This private function
    '          is called from the getOffers function.
    '
    ' Author : John Paraskevopoulos, 9 December 2012
    '===============================================================
    Private Function calcRank(ByVal row As DataRow) As Double
        Dim rankOfRow As Double = 0
        If (userMaster = row.Item("JobOfferMaster")) Then
            rankOfRow += CType(row.Item("JobOfferMasterWeight"), System.Double)
        End If

        If (userPhd = row.Item("JobOfferPhd")) Then
            rankOfRow += CType(row.Item("JobOfferPhdWeight"), System.Double)
        End If

        If (userFL = row.Item("JobOfferForeignLanguage")) Then
            rankOfRow += CType(row.Item("JobOfferForeignLanguageWeight"), System.Double)
        End If

        If (userArmy = row.Item("JobOfferArmy")) Then
            rankOfRow += CType(row.Item("JobOfferArmyWeight"), System.Double)
        End If

        'For the degree - Using distances values between degrees
        rankOfRow += row.Item("DegreesDistanceDistance") * row.Item("JobOfferDegreeWeight")

        'For the job type - Using distances values between Job Types
        rankOfRow += row.Item("JobTypesDistanceDistance") * row.Item("JobOfferJobTypeWeight")
        Return rankOfRow
    End Function

End Class
